<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id"
                :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="floor"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'showName',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/smis/floor/jfFloor/treeData"
                      :value="inputForm.floor.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.floor.id=value; $refs.selectPoint.showFloor(value); inputForm.x = ''; inputForm.y = ''; $refs.selectPoint.removeMarkPoint();}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="20" v-for="(item, index) in files" :key='index'>
             <el-form-item :label="item.label" :prop="item.value"
                  :rules="[
                   ]">
                <vr-upload-ext
                  :limit = "1"
                  :accept = "item.format"
                  :fileType = "item.fileType"
                  :width = "item.width"
                  :height = "item.height"
                  :outputSize = "0.7"
                  outputType = "jpeg"
                  :allowScale = "false"
                  :maxImageSize = "10485760"
                  v-model = "inputForm.file[item.value]"
                >
                </vr-upload-ext>
             </el-form-item>
        </el-col>
        <el-col :span="24" style="height: 400px;">
          <el-form-item label="所在位置" prop="x"
              :rules="[
                {required: true, message:'请选择所在位置', trigger:'blur'}
               ]">
            <select-point ref="selectPoint" type="vr" @onMapClick="mapClick"></select-point>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  // import ElUploadExt from '@/components/elUploadExt'
  import VrUploadExt from '@/components/vrUploadExt'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        files: [{
          label: '全景图',
          value: 'img',
          format: '.jpeg',
          fileType: '2',
          width: '2048',
          height: '1024'
        }],
        inputForm: {
          id: '',
          floor: {
            id: ''
          },
          name: '',
          x: '',
          y: '',
          file: {},
          filesAll: ''
        },

        // outputSize: {
        //   // 裁剪生成图片的质量 0.0~1.0
        //   type: Number,
        //   default: 1
        // },
        // outputType: {
        //   type: String,
        //   default: 'png', // 裁剪生成图片的格式
        // },

      }
    },
    components: {
      SelectTree,
      SelectPoint,
      VrUploadExt
    },
    methods: {
      init (method, id, floorId) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建VR全景点位`
        } else if (method === 'edit') {
          this.title = '修改VR全景点位'
        } else if (method === 'view') {
          this.title = '查看VR全景点位'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.file = {}
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/vr/jfVrList/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfVrList)
              if(data.jfVrList.filesAll && data.jfVrList.filesAll.length > 0) {
                this.inputForm.file = JSON.parse(data.jfVrList.filesAll || '{}')
              } else {
                this.inputForm.file = {}
              }
              this.loading = false
              this.$refs.selectPoint.showFloorAndMarkPoint( this.inputForm.floor.id, this.inputForm.x, this.inputForm.y, this.inputForm.z || 0 )
              // this.$refs.selectPoint.setMarkPoint( this.inputForm.floor.id, this.inputForm.x, this.inputForm.y)
            })
          } else if(floorId !== ''){
            this.inputForm.floor.id = floorId
            this.$refs.selectPoint.showFloor( floorId )
          }
          this.$refs.selectPoint.resize();
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.filesAll = JSON.stringify(this.inputForm.file)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/vr/jfVrList/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
		this.inputForm.z = e.z
      }
    }
  }
</script>
